import java.util.*;

public class Main {
    public static int[] solution(int n, int maxVal, int[] array) {
        Map<Integer, Integer> freq = new HashMap<>();
        for (int card : array) {
            freq.put(card, freq.getOrDefault(card, 0) + 1);
        }

        int[] order = {1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
        List<Integer> aCandidates = new ArrayList<>();
        List<Integer> bCandidates = new ArrayList<>();

        for (int card : order) {
            if (freq.getOrDefault(card, 0) >= 3) {
                aCandidates.add(card);
            }
            if (freq.getOrDefault(card, 0) >= 2) {
                bCandidates.add(card);
            }
        }

        for (int a : aCandidates) {
            for (int b : bCandidates) {
                if (b != a && (3 * a + 2 * b) <= maxVal) {
                    return new int[]{a, b};
                }
            }
        }

        return new int[]{0, 0};
    }

    public static void main(String[] args) {
        // Add your test cases here

        System.out.println(java.util.Arrays.equals(solution(9, 34, new int[]{6, 6, 6, 8, 8, 8, 5, 5, 1}), new int[]{8, 5}));
        System.out.println(java.util.Arrays.equals(solution(9, 37, new int[]{9, 9, 9, 9, 6, 6, 6, 6, 13}), new int[]{6, 9}));
        System.out.println(java.util.Arrays.equals(solution(9, 40, new int[]{1, 11, 13, 12, 7, 8, 11, 5, 6}), new int[]{0, 0}));
    }
}
